<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所属试卷id">
        <a-input placeholder="所属试卷id" v-decorator="['testPaperInfoId', {rules: [{required: true, message: '请输入所属试卷id'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="提交后是否立即出分">
        <a-select placeholder="请选择提交后是否立即出分" v-decorator="['commitAfterType', {rules: [{required: true, message: '请选择提交后是否立即出分'}]" allowClear>
          <a-select-option v-for="(b, index) in commitAfterTypeList" :key="index" :value="b.dictValue">{{ b.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否显示成绩排名榜">
        <a-input placeholder="是否显示成绩排名榜" v-decorator="['showMarkSort', {rules: [{required: true, message: '请输入是否显示成绩排名榜'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否显示学员成绩排名">
        <a-input placeholder="是否显示学员成绩排名" v-decorator="['showStudentSort', {rules: [{required: true, message: '请输入是否显示学员成绩排名'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="参考次数">
        <a-input placeholder="参考次数" v-decorator="['referenceNumber', {rules: [{required: true, message: '请输入参考次数'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="1.指定时间段 2. 永久有效">
        <a-select placeholder="请选择1.指定时间段 2. 永久有效" v-decorator="['examType', {rules: [{required: true, message: '请选择1.指定时间段 2. 永久有效'}]" allowClear>
          <a-select-option v-for="(b, index) in examTypeList" :key="index" :value="b.dictValue">{{ b.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveEPaperInfoSetting } from '@/api/ePaperInfoSetting'
import pick from 'lodash.pick'

export default {
  name: 'EPaperInfoSettingModal',
  props: {
    commitAfterTypeList: {
      type: Array,
      required: true
    },
    examTypeList: {
      type: Array,
      required: true
    }
  },
  components: {},
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate() {
  },
  created() {
  },
  methods: {
    add() {
      this.form.resetFields()
      this.edit({ id: 0 })
    },
    edit(record) {
      this.mdl = Object.assign(record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'testPaperInfoId', 'commitAfterType', 'showMarkSort', 'showStudentSort', 'createDate', 'updateDate', 'referenceNumber', 'startTime', 'endTime', 'examType'))
      })
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveEPaperInfoSetting(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
